package com.sh.promise;

import com.sh.promise.then.OnCatchedExecutor;
import com.sh.promise.then.OnCompleteListener;
import com.sh.promise.then.OnFulfilledExecutor;
import com.sh.promise.then.OnRejectedExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public interface IPromise extends Callable {

    /**
     *
     * @param onFulfilledExecutor  执行正常的回调函数
     * @return
     */
    public IPromise then(OnFulfilledExecutor onFulfilledExecutor);

    /**
     * @param onFulfilledExecutor  执行正常的回调函数
     * @param onRejectedExecutor   执行异常的回调函数
     * @return
     */
    public IPromise then(OnFulfilledExecutor onFulfilledExecutor, OnRejectedExecutor onRejectedExecutor);

    /**
     * then(null,onRejectedExecutor)的别名
     * @param onCatchedExecutor
     * @return
     */
    public IPromise pCatch(OnCatchedExecutor onCatchedExecutor);

    /**
     * 指定一个监听器，在promise状态转为fulfilled或rejected调用，该方法不会阻塞线程执行，可以多次调用指定多个监听器
     * @param onCompleteListener
     */
    public void addListener(OnCompleteListener onCompleteListener);

    /**
     * addListener的别名，行为同 addListener
     * @param onCompleteListener
     */
    public void pFinally(OnCompleteListener onCompleteListener);

    /**
     * 转变promise状态，只能由pending转变为fulfilled或rejected，
     * 如果promise已为fulfilled或rejected状态，操作将被忽略
     * @param status
     * @return
     */
    public boolean transformStatus(Status status,Object data);

    /**
     * 获取promise对应异步任务的future
     * @return
     */
    public Future getFuture();

    /**
     * 尝试取消promise对应的异步任务，底层调用future.cancel(true)。fulfilled或rejected状态下无效。
     * @return
     */
    public boolean cancel();
    /**
     * 获取当前状态
     * @return
     */
    public Status getStatus();

    /**
     * 获取fulfilled状态下的终值,其他状态为null
     * @return
     */
    public Object getResolvedData();

    /**
     * 获取promise rejected状态下的拒因，其余状态下为null
     * @return
     */
    public Throwable getRejectedData();

    public Object get();

    public void setSuccess(Object obj);

    public void setFailure(Throwable t);

    public void setResolvedData(Object obj);

    public void setRejectedData(Throwable t);
}
